package com.wgke.bluetooth.ui;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.newmobiler.android.ui.adapter.CellAdapter;
import com.newmobiler.android.ui.adapter.cell.Cell;
import com.newmobiler.android.ui.adapter.cell.DataBinder;
import com.newmobiler.android.ui.adapter.cell.MultiCell;
import com.newmobiler.android.ui.adapter.viewholder.RVViewHolder;
import com.wgke.bluetooth.R;
import com.wgke.bluetooth.vm.BaseVM;

import org.greenrobot.eventbus.EventBus;

import java.util.Arrays;
import java.util.List;

public abstract class BaseListActivity<VM extends BaseVM> extends BaseActivity<VM> {

    private CellAdapter cellAdapter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public void initView() {
        SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swipe_refresh);
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setColorSchemeResources(R.color.purple_200, R.color.purple_500, R.color.teal_200);
            Log.e("BaseListActivity", "initView:swipeRefreshLayout");
            swipeRefreshLayout.setOnRefreshListener(this::onRefresh);
        } else {
            Log.e("BaseListActivity", "initView:swipeRefreshLayout is null");
        }
        RecyclerView recyclerView = findViewById(R.id.tv_recycler);
        if (recyclerView != null) {
            cellAdapter = CellAdapter.createAdapter(recyclerView);
            //cellAdapter.setDataList(convert(getDataList()));
        }
        initOtherView();
    }

    public abstract int itemId();


    public <T> void setAdapter(List<T> list, DataBinder<T> dataBinder) {
        if (cellAdapter != null) {
            cellAdapter.setDataList(MultiCell.convert2(itemId(), list, dataBinder));
        }
    }

    public <T> void addCell(T list, DataBinder<T> dataBinder) {
        if (cellAdapter != null) {
            cellAdapter.addDataAtLast(MultiCell.convert(itemId(), list, dataBinder));
        }
    }

    public void setEmptyAdapter() {
        setEmptyAdapter("", 0);
    }

    public void setEmptyAdapter(String text, int resId) {
        if (cellAdapter != null) {
            Cell cell = MultiCell.convert(R.layout.layout_empty, text, (h, c) -> {
                if (!TextUtils.isEmpty(c)) {
                    h.setText(R.id.emptyTv, c);
                }
                if (resId != 0) {
                    h.setImageResource(R.id.emptyImage, resId);
                }
                h.getView(R.id.llAgain).setOnClickListener(v -> emptyAgain());
            });
            cellAdapter.setDataList(Arrays.asList(cell));
        }
    }

    public void emptyAgain() {

    }

    public void clear() {
        if (cellAdapter != null) {
            cellAdapter.setDataList(null);
        }
    }

    public void remove(int index) {
        if (cellAdapter != null) {
            if (index >= cellAdapter.getItemCount()) return;
            cellAdapter.removeDataAtIndex(index);
        }
    }


    public abstract void initOtherView();

    public abstract void onRefresh();

    public void finishRefresh() {
        SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swipe_refresh);
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(false);
        }
    }

    public void startRefresh() {
        SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swipe_refresh);
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(true);
        }
    }

    public void initToolbar(String title) {
        Toolbar toolbar = findViewById(R.id.toolbar);
        if (toolbar != null) {
            if (!TextUtils.isEmpty(title)) {
                toolbar.setTitle(title);
            }
            toolbar.setNavigationOnClickListener(v -> finish());
        }
    }

    public void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
        View view = getCurrentFocus();
        if (view == null) {
            view = new View(this);
        }
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}
